package cn.sciento.transfer.api.controller.v1;

import cn.sciento.core.domain.Page;
import cn.sciento.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import cn.sciento.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import cn.sciento.boot.platform.lov.annotation.ProcessLovValue;
import cn.sciento.core.base.BaseController;
import cn.sciento.core.util.Results;
import cn.sciento.mybatis.domian.SecurityToken;
import cn.sciento.mybatis.helper.SecurityTokenHelper;
import cn.sciento.starter.keyencrypt.core.Encrypt;
import cn.sciento.transfer.app.service.EventErrorService;
import cn.sciento.transfer.domain.entity.EventError;
import cn.sciento.transfer.domain.repository.EventErrorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController("eventErrorSiteController.v1")
@RequestMapping({"/v1/event-errors"})
public class EventErrorSiteController extends BaseController {
  @Autowired
  private EventErrorRepository eventErrorRepository;
  
  @Autowired
  private EventErrorService eventErrorService;
  
  @ApiOperation("事件错误监控列表")
  @Permission(level = ResourceLevel.SITE)
  @GetMapping
  @ProcessLovValue(targetField = {"body"})
  public ResponseEntity<Page<EventError>> list(@Encrypt EventError eventError, @ApiIgnore @SortDefault(value = {"eventId"}, direction = Sort.Direction.DESC) PageRequest pageRequest) {
    Page<EventError> list = this.eventErrorRepository.pageEventErrorByCondition(eventError, pageRequest);
    if (CollectionUtils.isNotEmpty(list.getContent()))
      list.getContent().forEach(EventError::generateId); 
    return Results.success(list);
  }
  
  @ApiOperation("事件错误监控明细")
  @Permission(level = ResourceLevel.SITE)
  @GetMapping({"/{eventId}"})
  @ProcessLovValue(targetField = {"body"})
  public ResponseEntity<EventError> detail(@Encrypt @PathVariable Long eventId) {
    EventError eventError = (EventError)this.eventErrorRepository.selectByPrimaryKey(eventId);
    return Results.success(eventError);
  }
  
  @ApiOperation("处理错误事件")
  @Permission(level = ResourceLevel.SITE)
  @PostMapping({"/process"})
  public ResponseEntity<EventError> process(@Encrypt @RequestBody EventError eventError) {
    SecurityTokenHelper.validToken((SecurityToken)eventError);
    return Results.success(this.eventErrorService.processEventError(eventError));
  }
}
